<template>
  <div>
    <!-- 定义面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-book :to="{ path: '/home' }">首页</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书管理</el-breadcrumb-book>
      <el-breadcrumb-book>{{"->"}}图书审核</el-breadcrumb-book>
    </el-breadcrumb>

    <!-- 定义卡片 -->
    <el-card class="box-card">

      <!-- 定义第一行数据 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryBookInfo.query" clearable @clear="getBookTempList">
            <el-button slot="append" icon="el-icon-search" @click="getBookTempList"></el-button>
          </el-input>
        </el-col>

      </el-row>
      <el-table :data="bookTempList" style="width: 100%" stripe border>

        <el-table-column type="index" align="center" label="序号" width="80px">
        </el-table-column>

        <el-table-column prop="stage" align="center" label="所在级" width="80px">
        </el-table-column>

        <el-table-column prop="name" align="center" label="书名" width="100px">
        </el-table-column>

        <el-table-column prop="author" align="center" label="作者名" width="100px">
        </el-table-column>

        <!-- 使用过滤器修改时间 在main.js中定义过滤器 -->
        <el-table-column prop="price" align="center" label="价格(元)" width="80px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.price | priceFormat}}
          </template>
        </el-table-column>

        <el-table-column prop="uploader" align="center" label="上传者" width="80px">
        </el-table-column>

        <el-table-column prop="intro" align="center" label="简介" width="120px">
        </el-table-column>

        <el-table-column prop="created" align="center" label="上传时间" width="200px">
          <template slot-scope="scope">
            <!-- {{scope.row.price | priceFormat}} -->
            {{scope.row.created | dateYMDHMSFormat}}
          </template>
        </el-table-column>

       <!-- <el-table-column prop="updated" label="更新时间" width="180px" :formatter="formatDate">
        </el-table-column> -->
        <el-table-column label="操作" align="center" width="400px">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="examBookBtn(scope.row)">审核通过</el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="disExamBookBtn(scope.row)">审核不通过</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 定义分页插件-->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryBookInfo.pageNum" :page-sizes="[1, 5, 10, 20]" :page-size="queryBookInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        //1.定义商品列表信息
        bookTempList: [],
        bookTemp: {
          id: '',
          stage:'',
          name:'',
          price:'',
          uploader:'',
          intro:'',
        },
        //2.定义分页对象
        queryBookInfo: {
          query: '', //定义查询参数
          pageNum: 1,
          pageSize: 10
        },
        total: 0,


      }
    },
    created() {
      //1.获取商品列表数据
      this.getBookTempList()
    },
    methods: {
      //实现审核书籍信息分页查询
      async getBookTempList() {
        const {
          data: result
        } = await this.$http.get("/bookTemp/getBookTempList", {
          params: this.queryBookInfo
        })
        if (result.status !== 200) return this.$message.error("书籍列表查询失败")
        this.bookTempList = result.data.rows
        this.total = result.data.total
      },

      //条数变化时 调用
      handleSizeChange(size) {
        this.queryBookInfo.pageSize = size
        this.getBookTempList()
      },
      //页码值变化时 调用
      handleCurrentChange(current) {
        this.queryBookInfo.pageNum = current
        this.getBookTempList()
      },
      //书籍通过审核
      async examBookBtn(bookTemp){
        //消息确认框
        const result = await this.$confirm('确定通过审核?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(error => error)
        if(result !== 'confirm'){
           return this.$message.info("取消审核")
        }
        const {data: result1} = await this.$http.get("/bookTemp/"+bookTemp.id)
        this.bookTemp = result1.data
        //审核表通过后,审核书籍已存到书籍数据库,所以在审核库中删除书籍
        await this.$http.delete(`/bookTemp/${result1.data.id}`)
        const {data: result2} = await this.$http.post("/bookTemp/addBookTemp", this.bookTemp)
        if (result2.status !== 200) return this.$message.error("书籍审核失败")
        this.$message.success("书籍审核成功")
        //重新获取书籍审核列表信息
        this.getBookTempList()
    },
    //书籍未通过审核
    async disExamBookBtn(bookTemp){
       //1.消息确认框
       const result =  await this.$confirm('是否确定该书不能通过审核?', '提示', {
                 confirmButtonText: '确定',
                 cancelButtonText: '取消',
                 type: 'warning'
               }).catch(error => error)
      if(result !== 'confirm'){
        return this.$message.info("已取消操作")
      }
      const {data: result1} = await this.$http.delete(`/bookTemp/${bookTemp.id}`)
      if(result1.status !== 200) return this.$message.error("操作失败")
      this.$message.success("操作成功")
      //重新加载 数据
      this.getUserList()
    },
    //利用钩子函数实现数据查询
    mounted(){
      this.getBookTempList()
    },
  },
}
</script>
<!-- 防止组件样式冲突 -->
<style lang="less" scoped>

</style>
